﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS
{
    public class SettingsDirectionDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private InsertInfoHandle Handle_Insert;
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        private int _Id;
        private string _SourcePos;
        private int _Direction;
        private string _TargetPos;
        private List<T_BasicInfo> list_BasicInfo = new List<T_BasicInfo>();

        //是否为编辑状态
        private bool _IsEdit;

        public string SourcePos
        {
            get => _SourcePos;
            set { _SourcePos = value; OnPropertyChanged(); }
        }

        public int Direction
        {
            get => _Direction;
            set { _Direction = value; OnPropertyChanged(); }
        }

        public int Id
        {
            get => _Id; set { _Id = value; OnPropertyChanged(); }
        }

        public string TargetPos
        {
            get => _TargetPos; set { _TargetPos = value; OnPropertyChanged(); }
        }

        public bool IsEdit
        {
            get => _IsEdit; set { _IsEdit = value; OnPropertyChanged(); }
        }

        //提交
        public RelayCommand ApplyCommand { get; }

        private ObservableCollection<string> directionOptions;

        public ObservableCollection<string> DirectionOptions
        {
            get => directionOptions;
            set { directionOptions = value; OnPropertyChanged(); }
        }

        private string _SelectedOption = string.Empty;

        public string SelectedOption
        {
            get => _SelectedOption;
            set
            {
                _SelectedOption = value;
                OnPropertyChanged();
            }
        }

        public List<T_BasicInfo> List_BasicInfo
        {
            get => list_BasicInfo;
            set
            {
                list_BasicInfo = value;
                OnPropertyChanged();
            }
        }

        #endregion 属性

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="SelectItem"></param>
        /// <param name="IsEdit"></param>
        public SettingsDirectionDialogViewModel(SetttingDirectionModel SelectItem, Boolean IsEdit)
        {
            this.IsEdit = IsEdit;
            ApplyCommand = new RelayCommand(Apply);
            commonHandle = new CommonHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            InitDirectionOptions();
            List_BasicInfo = Handle_Search.GetBasicInfo();
            if (SelectItem != null)
            {
                this.Id = SelectItem.Id;
                this.SourcePos = SelectItem.SourcePos;
                this.TargetPos = SelectItem.TargetPos;
                var ii = List_BasicInfo.FirstOrDefault(s => s.Value.Equals(SelectItem.Direction.ToString())).Id;
                SelectedOption = DirectionOptions[ii - 1];
            }
        }

        /// <summary>
        /// 提交函数
        /// </summary>
        private void Apply()
        {
            //利用数据库查询获得基础信息
            Direction = int.Parse(List_BasicInfo.FirstOrDefault(s => s.Describe.Equals(SelectedOption)).Value);

            var t = new T_DirectionConfig
            {
                SourcePos = SourcePos,
                TargetPos = TargetPos,
                Direction = Direction
            };

            if (IsEdit)
            {

            }
            else
            {

            }
            commonHandle.CloseDiaLog(GetLocalText("SettingsDirectionDialog"));
        }

        private void InitDirectionOptions()
        {
            DirectionOptions = new ObservableCollection<string>
            {
                GetLocalText("Forward"),
                GetLocalText("Turn Left"),
                GetLocalText("Turn Right"),
                GetLocalText("Forward Left"),
                GetLocalText("Forward Right"),
                GetLocalText("Forward Left Wide"),
                GetLocalText("Forward Right Wide"),
                GetLocalText("Forward Long"),
                GetLocalText("NG"),
                GetLocalText("Off"),
            };
        }
    }
}